package cn.zniot.so.application.util;

import cn.zniot.so.application.constant.Constants;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import java.io.File;

/**
 * 加载属性文件
 */
public class LoadConf {

	/**
	 * 配置文件可能存在的目录 相对于HomeDir的目录
	 */

	private static final String LOG4J_PROPERTIES = "log4j.properties";

	private static final String APPLICATION_PROPERTIES = "application.xml";

	/**
	 * 加载属性文件
	 * 
	 * @param homeDir
	 */
	public static void init(String homeDir) {
		homeDir = homeDir + "/conf";
		// log4j配置文件
		String log4jPropertiesFileUrl = null;
		
		// 应用程序配置文件
		String appPropertiesFileUrl = null;
		
		/*
		 * 先找到配置文件的fileurl
		 */
		if (log4jPropertiesFileUrl == null) {
			log4jPropertiesFileUrl = getFileUrl(homeDir,
					LOG4J_PROPERTIES);
			if (!isPropertiesFileExists(log4jPropertiesFileUrl)) {
				log4jPropertiesFileUrl = null;
			}
		}
		if (appPropertiesFileUrl == null) {
			appPropertiesFileUrl = getFileUrl(homeDir,
					APPLICATION_PROPERTIES);
			if (!isPropertiesFileExists(appPropertiesFileUrl)) {
				appPropertiesFileUrl = null;
			}
		}
		if (log4jPropertiesFileUrl != null) {
			PropertyConfigurator.configure(log4jPropertiesFileUrl);

			Log logger = LogFactory.getLog(LoadConf.class);
			if (logger.isDebugEnabled()) {
				logger.debug("Init log4j properties succeed...");
			}
		} else {
			System.err.println(" --------------------------------------------------------- ");
			System.err.println(" --- Try to find the properties file of LOG4J Failed...");
			System.err.println(" --- Please check whether your properties file of LOG4J in your "
							+ homeDir
							+ " or "
							+ homeDir
							+ "/properties or "
							+ homeDir + "/prop directory exist.");
			System.err.println(" --------------------------------------------------------- ");
		}

		Log logger = LogFactory.getLog(LoadConf.class);
		if (appPropertiesFileUrl != null) {
			try {
				Configuration config = new XMLConfiguration(
						appPropertiesFileUrl);
				Constants.initProperties(config);
			} catch (ConfigurationException e) {
				logger.error("加载appliaction配置文件[" + appPropertiesFileUrl
						+ "]异常::", e);
			}
		} else {
			System.err.println(" --------------------------------------------------------- ");
			System.err.println(" --- Try to find the properties file of Application Failed...");
			System.err.println(" --- Please check whether your properties file of Application in your "
							+ homeDir
							+ " or "
							+ homeDir
							+ "/properties or "
							+ homeDir + "/prop directory exist.");
			System.err.println(" ---### Application will exit now! ###--- ");
			System.err.println(" ---###  Startup Failed!   System exit...###---");
			System.err.println(" --------------------------------------------------------- ");
			System.exit(0);
		}
	}

	
	/**
	 * 获取文件路径
	 * 
	 * @param path
	 * @param name
	 * @return
	 */
	private static String getFileUrl(String path, String name) {
		return path + "/" + name;
	}

	/**
	 * 检查文件是否存在
	 * 
	 * @param fileUrl
	 * @return
	 */
	private static boolean isPropertiesFileExists(String fileUrl) {
		File file = new File(fileUrl);
		return file.isFile();
	}
	
	public static void main(String[] arg){
		String ids = "1|2"; 
		String rates = "10|20"; 
		
		// 
		String[] arrRateId = ids.split("\\|");//要转义否则会有问题的
		String[] arrRate = rates.split("\\|");
		int len = arrRateId.length > arrRate.length ? arrRate.length
				: arrRateId.length;
		for (int i = 0; i < len; i++) {
//			ID_RATES_MAP.put(arrRateId[i], arrRate[i]);
		}
	}
}
